package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.FragmentViewPagerAdapter;
import com.chexiehui.jc.base.BaseFragment;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;

import butterknife.BindView;

public class SearchResultFragment extends BaseFragment {

    public static SearchResultFragment getInstance(String keyword) {
        SearchResultFragment fragment = new SearchResultFragment();
        Bundle bundle = new Bundle();
        bundle.putString("keyword", keyword);
        fragment.setArguments(bundle);
        return fragment;
    }

    @BindView(R.id.stl_action_table)
    SlidingTabLayout stlActionTable;
    @BindView(R.id.vp_action_pager)
    ViewPager vpActionPager;

    private String keyword;
    private String[] titles = new String[]{"综合", "帖子", "论坛", "二手车"};
    private ArrayList<Fragment> forumFragmentList = new ArrayList<>();
    private FragmentViewPagerAdapter pagerAdapter;
    @Override
    protected void beforeSetView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            keyword = bundle.getString("keyword");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_search_result;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        forumFragmentList = new ArrayList<>();
        forumFragmentList.add(SearchResultAllFragment.getInstance(keyword));
        forumFragmentList.add(SearchResultPostsFragment.getInstance(keyword));
        forumFragmentList.add(SearchResultForumFragment.getInstance(keyword));
        forumFragmentList.add(SearchResultCarAssistantFragment.getInstance(keyword));
        stlActionTable.setSnapOnTabClick(true);
        vpActionPager.setOffscreenPageLimit(titles.length);
        pagerAdapter = new FragmentViewPagerAdapter(getChildFragmentManager(), forumFragmentList, titles);
        vpActionPager.setAdapter(pagerAdapter);
        stlActionTable.setViewPager(vpActionPager);
    }

}
